package lex;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class Dump
{
	private String text;
	private List<Token> matches;

	public Dump(String text, List<Token> matches)
	{
		this.text = text;
		this.matches = matches;
	}

	/**
	 * prints the tokenized token list to a .tok file 
	 * as a table using String.format()
	 * 
	 * @throws IOException 	
	 * @throws FileNotFoundException 
	 */
	public void printTableToFile() throws FileNotFoundException, IOException {


		try {
			String outputFilename = text.replace(".ic", ".tok");
			PrintWriter writer = new PrintWriter(outputFilename, "ASCII"); //using ASCII, same as input .ic file
			writer.println(String.format("%1s%28s%16s%8s%10s\n","token" ,"tag" ,"line",   ":", "column" ));

			for (int i = 0; i < matches.size(); i++) {
				Token cur = matches.get(i);
				String tempValue = padSpacesStringToLen(cur.value, 16);
				String tempTag = padSpacesStringToLen(cur.tag, 16);
				writer.println(String.format("%s%s%16d%1s%16d\n",tempValue ,tempTag ,cur.line," " , cur.column));
			}
			writer.close();
		}
		catch (IOException e) {
			System.err.println(e);
		}
		catch (Exception e) {
			System.err.println(e);
		}
	}

	/**
	 * prints the tokenized table to console
	 */
	public void consolePrintTable() {

		System.out.format("%1s%16s%16s%8s%10s\n","token" ,"tag" ,"line",   ":", "column" );
	
		for (int i = 0; i < matches.size(); i++) {
			Token cur = matches.get(i);
			String tempValue = padSpacesStringToLen(cur.value, 16);
			String tempTag = padSpacesStringToLen(cur.tag, 16);
			System.out.format("%1s%16s%4d%8s%10d\n",tempValue ,tempTag ,cur.line," " , cur.column);
		}
	}
	
	/**
	 * 
	 * @param s
	 * @param len
	 * @return s with whitespaces extention 'till s.length = n
	 */
	private String padSpacesStringToLen(String s, int targetLen)
	{
		int iterCount = targetLen - s.length();
		if (iterCount > 0)
		{
			while (iterCount > 0)
			{
				s += " ";
				iterCount--;
			}
		}
		
		else
			s += "\t";
				
		return s;
	}


	/**
	 * legacy from recitation
	 */
	/*
	public String console()
	{
		StringBuilder sb1 = new StringBuilder();
		StringBuilder sb2 = new StringBuilder();
		for (int i = 0; i < text.length(); i++) {
			sb1.append(" " + text.charAt(i));
			char s = stateAt(i);
			sb2.append((s == '-' || s == ']' ? "-" : " ") + s);
		}
		return sb1.toString() + "\n" + sb2.toString();
	}

	public String html()
	{
		StringBuilder sb1 = new StringBuilder();
		StringBuilder sb2 = new StringBuilder();
		StringBuilder sb3 = new StringBuilder();
		for (int i = 0; i < text.length(); i++) {
			char c = text.charAt(i);
			String sc = (c == ' ') ? "&nbsp;" : (""+c);
			sb1.append(String.format("<td>%s</td>", sc));
			String cls;
			switch (stateAt(i)) {
			case '-': cls = "mid"; break;
			case '[': cls = "first"; break;
			case ']': cls = "last"; break;
			case 'I': cls = "first last"; break;
			default: cls = "";
			}
			int num = matchAt(i);
			if (num >= 0) cls += (" clr" + (num % 7)); 
			sb2.append(String.format("<td class='%s'>&nbsp;</td>", cls));
		}
		int pos = 0;
		for (Token tok : matches) {
			if (tok.start > pos) sb3.append(String.format("<td colspan='%d'></td>", tok.start-pos));
			sb3.append(String.format("<td colspan='%d'>%s</td>", tok.end-tok.start, tok.tag));
			pos = tok.end;
		}
		return String.format("<table>\n" + 
				"  <tr class='input'>%s</tr>\n  <tr class='matches'>%s</tr>" +
				"  <tr class='tags'>%s</tr>\n</table>", sb1, sb2, sb3);
	}

	private char stateAt(int index) {
		for (Token m : matches) {
			if (index == m.start) return (index == m.end-1) ? 'I' : '[';
			else if (index == m.end-1) return ']';
			else if (index > m.start && index < m.end-1) return '-';
		}
		return ' ';
	}

	private int matchAt(int index) {
		for (int mi = 0; mi < matches.size(); mi++) {
			Token m = matches.get(mi);
			if (m.start <= index && index < m.end) {
				return mi;
			}
		}
		return -1;
	}*/
}
